/* 
 * Copyright 2011-2012 Andrew Brock
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.catchwa.myvideobrowser.gui;

import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import org.catchwa.myvideobrowser.Main;
import org.catchwa.myvideobrowser.model.Movie;

/**
 *
 * @author Andrew Brock
 */
public class MovieInfoPanel extends JPanel implements ListSelectionListener {

  private JTable table;
  private String currentSelection;
  private JTextPane textPane;
  private JScrollPane textAreaScrollPane;

  public MovieInfoPanel(JTable table) {
    this.table = table;
    currentSelection = null;
    textPane = new JTextPane();
    textPane.setEditable(false);
    textPane.setContentType("text/html");
    textAreaScrollPane = new JScrollPane(textPane);
    textAreaScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
    this.setLayout(new BorderLayout());
    this.add(textAreaScrollPane, BorderLayout.CENTER);
  }

  @Override
  public void valueChanged(ListSelectionEvent e) {
    if (table.getSelectedRow() >= 0) {
      int viewRow = table.getSelectedRow();
      int modelRow = table.convertRowIndexToModel(viewRow);
      String selectedTitle = (String) table.getModel().getValueAt(modelRow, 0);
      if (currentSelection == null || ! currentSelection.equals(selectedTitle)) {
        Movie m = Main.db.get(selectedTitle);
        currentSelection = selectedTitle;
        setTextFromMovie(m);
      }
    }
  }

  private void setTextFromMovie(Movie m) {
    StringBuilder sb = new StringBuilder();
    sb.append("<html>");
    sb.append("<h2>").append(m.getTitle()).append(" (").append(m.getReleasedYear()).append(")").append("</h2>");
    sb.append("<br>");
    sb.append("<img src=\"").append(m.getPosterUrl()).append("\" />");
    sb.append("<br>");
    sb.append("<strong>").append("Released: ").append("</strong>").append(m.getReleased());
    sb.append("<br>");
    sb.append("<strong>").append("Runtime: ").append("</strong>").append(m.getRuntime()).append(" minutes");
    sb.append("<br>");
    sb.append("<strong>").append("TMDb Rating: ").append("</strong>").append(m.getTmdbRating());
    sb.append("<br>");
    sb.append("<strong>").append("Overview: ").append("</strong>").append(m.getOverview());
    sb.append("</html>");
    textPane.setText(sb.toString());
  }

  public void clear() {
    textPane.setText("");
  }
}
